import Secret from "./secret";
var EncryptUtil = {};
/**
 * 加密参数
 */
EncryptUtil.encryptWord=function(word,encodeUri){
  word=Secret.Encrypt(word);
  if(!encodeUri){
    word=encodeURIComponent(word);
  }
  return word;
}
/**
 * 解密参数
 */
EncryptUtil.decryptWord=function(word,decodeUri){
  if(!decodeUri){
    word=decodeURIComponent(word);
  }
  return Secret.Decrypt(word);
}

/**
 * 根据key 从 localStorage 获取数据
 * @param {Object} key
 */
EncryptUtil.getKey = function (key) {
  return localStorage.getItem(key);
};

/**
 * 将数据存放到 localStorage 中.
 * @param {Object} key
 * @param {Object} val
 */
EncryptUtil.setKeyVal = function (key, val) {
  var type = EncryptUtil.getObjType(val);
  if (type == "object" || type == "array") {
    val = JSON.stringify(val);
  }
  localStorage.setItem(key, val);
};

EncryptUtil.getObjType = function (obj) {
  var toString = Object.prototype.toString;
  var map = {
    '[object Boolean]': 'boolean',
    '[object Number]': 'number',
    '[object String]': 'string',
    '[object Function]': 'function',
    '[object Array]': 'array',
    '[object Date]': 'date',
    '[object RegExp]': 'regExp',
    '[object Undefined]': 'undefined',
    '[object Null]': 'null',
    '[object Object]': 'object'
  };
  if (obj instanceof Element) {
    return 'element';
  }
  return map[toString.call(obj)];
};

/**
 * 判断数据是否为空。
 * @param {Object} val
 */
EncryptUtil.isEmpty = function (val) {
  if (val == undefined) return true;
  if (val == null) return true;
  if (typeof val == "string") {
    return val.trim() == "";
  }
  return false;
}

export default EncryptUtil;